package com.tencent.wework.demo

import androidx.lifecycle.ViewModelProviders
import com.tencent.wework.demo.base.CommonListFragment
import com.tencent.wework.list.BaseRecyclerCell
import com.tencent.wework.list.LoadMoreDirection
import kotlinx.android.synthetic.main.base_list_fragment.*

/**
 * Created by huangwei on 2019/5/27.
 */
class DemoFragment(): CommonListFragment<List<ItemData>, DemoViewModel>() {

    /**
     * 可选，配置项
     */
    init {
        loadMoreDirection = LoadMoreDirection.BOTTOM
        viewpagerMode = false
    }

    override fun initData() {
        super.initData()
        recyclerView.postDelayed({
            viewModel.requestData()
        },5000
        )
        recyclerView.postDelayed({
            adapter.notifyDataSetChanged()
        },7000
        )
    }
    override fun initViewModel(): DemoViewModel {
        return ViewModelProviders.of(this).get(DemoViewModel::class.java)
    }

    override fun createCells(data: List<ItemData>): List<BaseRecyclerCell<out Any>> {
        return data.mapIndexed {
            index,item->
            if(index%2==0) DemoItemCell(item,viewpagerMode) else DemoItemCell2(item,viewpagerMode)
        }
    }

    /**
     * 可选，实现加载更多功能
     */
    override fun getOffset(data: List<ItemData>, fromLoadMore:Boolean): Any? {
       return adapter.itemCount
    }

    /**
     * 可选，实现加载更多功能
     */
    override fun checkNoMore(data: List<ItemData>, fromLoadMore: Boolean): Boolean {
        return data.isEmpty()
    }
}

